#include <iostream>
#include <vector>
#include <string>

using namespace std;

int hammingDistance(int x, int y) {
    return __builtin_popcount(x ^ y);
}

int myHammingDistance(int x, int y) {
    int s = x ^ y, ret = 0;
    while (s) {
        ret += s & 1;
        s >>= 1;
    }
    return ret;
}

int myTotalHammingDistance(vector<int>& nums) {
    int total = 0;
    for(auto i = nums.begin(); i != nums.end(); i++){
        for(auto j = i+1; j != nums.end(); j++){
            total += hammingDistance(*i, *j);
        }
    }
    return total;
}

int totalHammingDistance(vector<int> &nums) {
    int ans = 0, n = nums.size();
    for (int i = 0; i < 30; ++i) {
        int c = 0;
        for (int val : nums) {
            c += (val >> i) & 1;
        }
        ans += c * (n - c);
    }
    return ans;
}

int main(){
    vector<int> nums = {4, 14, 2};
    cout << totalHammingDistance(nums) << endl;
    return 0;
}